package flib;

import java.util.Vector;

import matrixMath.MatrixMath;
import analyses.DepthAnalyser2D;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;
import simpleGeom.Bounds3D;

public class LevelAnalyser {
	PApplet parent;
	float sX;
	float sY;
	float dX;
	float dY;Bounds3D b3d;
	PGraphics g2d;
	PGraphics analyse;
public void calculateWall(FlibLevel level,float res){
	 b3d=level.getBounds3D();
	 dX=b3d.getDimX();
	 dY=b3d.getDimY();
	float sX=b3d.getX1();
	float sY=b3d.getY1();
	int nX=(int)(dX/res)+1;
	int nY=(int)(dY/res)+1;
	g2d=(PGraphics) parent.createGraphics(nX,nY,PApplet.P2D);
	g2d.beginDraw();
	g2d.background(255);
	g2d.scale(1f/res);
	g2d.translate(-sX, -sY);
	g2d.noStroke();
	g2d.fill(255);
	for (int i=0;i<level.rooms.size();i++){
		FlibFace room=level.rooms.get(i);
		room.display(g2d);
	}
	g2d.stroke(0);
	for (int i=0;i<level.walls.size();i++){
		FlibWall wall=level.walls.get(i);
		g2d.stroke(i);
		if (wall.type==null){
			g2d.stroke(255,255,0);
			g2d.line(wall.x1(),wall.y1(),wall.x2(),wall.y2());
		}
		else if (!wall.type.permeable||wall.type.label.equals("stair-end")||wall.getR1()==null||wall.getR2()==null){
			g2d.line(wall.x1(),wall.y1(),wall.x2(),wall.y2());
		}
	}
	g2d.endDraw();
	Integer[] values=new Integer[255];
	for (int i=0;i<255;i++){
		values[i]=new Integer(i);
	}
	Object[][] startDepth=new Object[g2d.width][g2d.height];
	for (int x=0;x<g2d.width;x++){
		for (int y=0;y<g2d.height;y++){
			int col=g2d.get(x, y);
			float b=parent.brightness(col);
			if (b<255){
				startDepth[x][y]=values[(int)b];
			}
			else{
				startDepth[x][y]=null;
			}
		}
	}
	DepthAnalyser2D dA=new DepthAnalyser2D();
	dA.calculateObjectDepth(startDepth);
	Object[][] distribution =dA.getDistribution();
	analyse=(PGraphics) parent.createGraphics(nX,nY,PApplet.P2D);
	analyse.beginDraw();
	analyse.colorMode(PApplet.HSB);
	
	for (int x=1;x<distribution.length-1;x++){
		for (int y=1;y<distribution[x].length-1;y++){
			Integer cI=(Integer) distribution[x][y];
			Vector<Integer>nbs=new Vector<Integer>();
			for (int cX=x-1;cX<x+2;cX++){
				for (int cY=y-1;cY<y+2;cY++){
					Integer nI=(Integer) distribution[cX][cY];
					if (nI!=null){
						if (!nbs.contains(nI)){
							nbs.add(nI);
						}
					}
				}
			}
			/*if (cI==null){
				analyse.set(x,y,analyse.color(255,0,0));
			}
			else{
				analyse.set(x,y,analyse.color(cI,255,255));
			}*/
			if (nbs.size()>1){
				analyse.set(x,y,analyse.color(255,255,255));
			}
			
		}
	}
	
	/*for (int x=0;x<distribution.length;x++){
		for (int y=0;y<distribution[x].length;y++){
			Integer cI=(Integer) distribution[x][y];
			if (cI==null){
				analyse.set(x,y,analyse.color(255,0,0));
			}
			else{
				analyse.set(x,y,analyse.color(cI,255,255));
			}
			
		}
	}*/
	analyse.endDraw();
}

public void calculateDepth(FlibLevel level,float res){
	 b3d=level.getBounds3D();
	 dX=b3d.getDimX();
	 dY=b3d.getDimY();
	float sX=b3d.getX1();
	float sY=b3d.getY1();
	int nX=(int)(dX/res)+1;
	int nY=(int)(dY/res)+1;
	g2d=(PGraphics) parent.createGraphics(nX,nY,PApplet.P2D);
	g2d.beginDraw();
	g2d.background(0);
	g2d.scale(1f/res);
	g2d.translate(-sX, -sY);
	g2d.noStroke();
	g2d.fill(255);
	for (int i=0;i<level.rooms.size();i++){
		FlibFace room=level.rooms.get(i);
		room.display(g2d);
	}
	g2d.stroke(0);
	for (int i=0;i<level.walls.size();i++){
		FlibWall wall=level.walls.get(i);
		if (!wall.type.permeable||wall.type.label.equals("stair-end")||wall.getR1()==null||wall.getR2()==null){
			g2d.line(wall.x1(),wall.y1(),wall.x2(),wall.y2());
		}
		
	}
	g2d.endDraw();
	double[][] startDepth=new double[g2d.width][g2d.height];
	for (int x=0;x<g2d.width;x++){
		for (int y=0;y<g2d.height;y++){
			int col=g2d.get(x, y);
			float b=parent.brightness(col);
			if (b>5){
				startDepth[x][y]=1000;
			}
			else{
				startDepth[x][y]=0;
			}
		}
	}
	double[][] depth=DepthAnalyser2D.getDepth(startDepth);
	double max=MatrixMath.max(depth);
	analyse=(PGraphics) parent.createGraphics(nX,nY,PApplet.P2D);
	analyse.beginDraw();
	analyse.colorMode(PApplet.HSB);
	
	System.out.println("max: "+max);
	for (int x=0;x<depth.length;x++){
		for (int y=0;y<depth[x].length;y++){
			double cD=depth[x][y];
			//System.out.println(cD);
			float c=PApplet.map((float)cD, 0, (float)max, 0, 255);
			int col=analyse.color(c,255,255);
			if (cD>0){
			analyse.set(x,y,col);
			}
		}
	}
	
	
	analyse.endDraw();
}
}
